package com.banciyuan.bcywebview.biz.detail.noveldetail;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.NovelApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BookNovelHelper {

    public static void doBook(final Context context, final boolean isBook, String wns_id, final NovelBookSuccess novelBookSuccess) {
        String url = isBook ? HttpUtils.BASE_URL + NovelApi.getSubscribe() : HttpUtils.BASE_URL + NovelApi.getUnsubscribe();
        List<BcyPair> BcyPairList = new ArrayList<>();
        BcyPairList.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(context).getToken()));
        BcyPairList.add(new BcyPair(HttpUtils.WNS_ID, wns_id));
        Map<String, String> map = HttpUtils.getData(BcyPairList);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, context)) {
                    if (null != novelBookSuccess)
                        novelBookSuccess.onBookSuccess();
                } else {
                    if (null != novelBookSuccess)
                        novelBookSuccess.onBookError();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                MyToast.show(context, context.getString(R.string.operation_fail));
                if (null != novelBookSuccess)
                    novelBookSuccess.onBookError();
            }
        };
        OnErrorProxy errorProxy = new OnErrorProxy(onError, onSucc, url, context, map);
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, errorProxy);
        VolleyQueue.getRquest(context).add(requestParam);
    }

    public interface NovelBookSuccess {
        void onBookSuccess();

        void onBookError();
    }
}
